package org.joseph.springcloud.controller;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.joseph.springcloud.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@Controller
public class UserConsumerController {

    final String url = "http://SPRINGCLOUD-PROVIDER-DEPT";

    @Autowired
    private RestTemplate restTemplate;

    @RequestMapping("/user/{id}")
    @ResponseBody
    @HystrixCommand(fallbackMethod="queryByIdBak")
    public User queryById(@PathVariable("id") long id){
        return restTemplate.getForObject(url+"/user/queryById/"+id, User.class);
    }

    public User queryByIdBak(@PathVariable("id") long id){
        return new User().setId(0L).setUsername("Service stop");
    }

    @RequestMapping("/getuser/{id}")
    @ResponseBody
    public User getUser(@PathVariable("id") long id){
        return restTemplate.getForObject(url+"/getuser/"+id, User.class);
    }
}
